# -*- coding: utf-8 -*-
"""
Created on Thu Jul 27 11:55:49 2017

@author: ZFH
"""

import pandas as pd
from collections import defaultdict
import numpy as np

price = []

for i in range(1880,2017):
    file_name = r'D:\data-set\names\yob%d.txt' % i
    frame = pd.read_csv(file_name, names=['name', 'sex', 'births'])
    frame['year'] = i
    price.append(frame)

names = pd.concat(price, ignore_index=True)
#　count_names = [name for name in names1880['name']]
total_brith = names.pivot_table(names, index=['year','sex'],aggfunc=sum)

# total_brith.plot()
# print(total_brith.tail)

def add_prop(group):
    births = group.births.astype(float)
    group['prop'] = births/births.sum()
    return group

names = names.groupby(['year','sex']).apply(add_prop)

